package linuxbox.reservation.bean;

import java.util.*;
import java.sql.Timestamp;

import linuxbox.util.*;
import linuxbox.exception.*;
import linuxbox.bean.*;

import linuxbox.reservation.logic.BoxReservationRepeatEntry;

public class BoxReservationRepeatBean extends BoxReservationRepeatEntry implements ITableBean
{
  protected DbAdapter m_db = null;
  protected boolean m_bTransaction = false;
	static protected String m_TableName = "box_reservation_repeat";
  
	public BoxReservationRepeatBean(BaseIntKey pk, DbAdapter db)
	{
		this.init(pk, db);
	}

	public BoxReservationRepeatBean(BaseIntKey pk)
	{
		this.init(pk, null);
	}

	private	void init(BaseIntKey pk, DbAdapter db)
	{
		if (db != null) 
		{
			m_db = db;
			m_bTransaction = true;
		}
		if (pk != null)
			setID(pk.getID());
	}
	
  public String getTableName()
  {
    return m_TableName;
  }
  
  public void setTableName(String str)
  {
    m_TableName = str;
  }

	public void makeConnection() throws Exception
	{
		if ( m_db == null )
      m_db = new DbAdapter(true);
	}
	
	public void closeConnection() throws Exception
	{
	  if (m_db != null)
	    m_db.close();
	}

	public void load() throws Exception
	{
		StringBuffer sql = new StringBuffer();

  	sql.append("select id, type, until, every, dayofweek, whichday from ");
    sql.append(getTableName());
    sql.append(" where id = " + getID());
    
    makeConnection();
		m_db.executeQuery(sql.toString());
		if (m_db.next())
		{
	    m_iID = m_db.getInt("id");
      m_iType = m_db.getInt("type");;
      m_UntilTime = m_db.getTimestamp("until");
      m_iEvery = m_db.getInt("every");
      m_DayOfWeek = m_db.getInt("dayofweek");
      m_iWhichDay = m_db.getInt("whichday");
		}
		else
		{
		  if (!m_bTransaction)
			  closeConnection();
      throw new LinuxboxException(ExceptionHelper.RESERVATION, "LOAD_REPEAT", "Can't load repeat Calendar record(" + m_iID + ")");
		}
	  if (!m_bTransaction)
  		closeConnection();
	}

	public void store() throws Exception
	{
	  StringBuffer sql = new StringBuffer();
		sql.append("UPDATE ");
		sql.append(getTableName());
		sql.append(" set ");
	  DBUtil.setInteger(sql, "type", m_iType, true);
		DBUtil.setInteger(sql, "every", m_iEvery, false);
		DBUtil.setInteger(sql, "dayofweek", m_DayOfWeek, false);
		DBUtil.setInteger(sql, "whichday", m_iWhichDay, false);
		DBUtil.setTimestamp(sql, "until", m_UntilTime, false);
	
		sql.append(" where id = " + m_iID);
		             
//System.out.println(sql);

		makeConnection();
		m_db.executeUpdate(sql.toString());
	  if (!m_bTransaction)
  		closeConnection();
	}

	public void refresh() throws Exception
	{
		load();
	}

	public void create() throws Exception
	{
		if (m_iID != 0)
		{
      String sql = "INSERT INTO ";
      sql += getTableName();
      sql += " (id) VALUES(" + m_iID + ")";
			makeConnection();
			m_db.executeUpdate(sql);
		}
		else
		{
			String sql = "select max(id) from ";
      sql += getTableName();
			makeConnection();
			m_db.executeQuery(sql);
			if (m_db.next())
			{
        int max_id = m_db.getInt(1);
        max_id++;
        m_iID = max_id;
			}
      else
      {
        m_iID = 1;
      }
      sql = "insert into ";
      sql += getTableName();
      sql += " (id) values(" + m_iID + ")";
      m_db.executeUpdate(sql);
		}
	  if (!m_bTransaction)
  		closeConnection();
	}

	public void remove() throws Exception
	{
		String sql = "DELETE FROM ";
    sql += getTableName();
    sql += (" where id = " + m_iID);
		makeConnection();
		m_db.executeUpdate(sql);
	  if (!m_bTransaction)
  		closeConnection();
	}

	public Object findByKey() throws Exception
	{
		String sql = "select id from ";
    sql += getTableName();
    sql += (" where id = " + m_iID);
		makeConnection();
		m_db.executeQuery(sql);
		BaseIntKey pk = null;
		if(m_db.next())
		{
			pk = new BaseIntKey();
			pk.setID(m_db.getInt(1));
		}
	  if (!m_bTransaction)
    	closeConnection();
		return pk;
	}

	public Iterator findBySql(String sqlWhere)	throws Exception
	{
		Vector v = null;
		try
		{
			String sql = "select id from ";
      sql += getTableName();
      sql += sqlWhere;
//System.out.println(sql);
			makeConnection();
			m_db.executeQuery(sql);
			BaseIntKey pk = null;
			v = new Vector();
			while (m_db.next())
			{
				pk = new BaseIntKey();
				pk.setID(m_db.getInt(1));
				v.addElement(pk);
//System.out.println(pk.getID());
			}
		  if (!m_bTransaction)
  			closeConnection();
		}
		catch(Exception e)
		{
		  if (!m_bTransaction)
  			closeConnection();
      throw new LinuxboxException(ExceptionHelper.RESERVATION, "FIND_REPEAT", "Can't find record: " + e.getMessage());
		}
		return v.iterator();
	}	
}